package com.woniuxy.lab.woniu_lab_boss.common;

import com.baomidou.mybatisplus.extension.api.R;
import com.woniuxy.lab.woniu_lab_boss.exception.WebException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestValueException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.util.List;
import java.util.stream.Collectors;

// 全局异常处理
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(WebException.class)
    public Result<Object> handleWebException(WebException e) {
        e.printStackTrace();// 打印堆栈信息
        return Result.fail(e.getCode(), e.getMessage());
    }
    //请求参数错误
    @ExceptionHandler(TypeMismatchException.class)
    public Result<Object> TypeMismatchException(TypeMismatchException e){
        e.printStackTrace();// 打印堆栈信息
        return Result.fail(5008, "请求参数格式不合法");
    }
    @ExceptionHandler(Exception.class)
    public Result<Object> handleException(Exception e) {
        e.printStackTrace();
        return Result.fail(500, "服务器繁忙");
    }

    @ExceptionHandler(BadCredentialsException.class)
    public Result<Object> handleException(BadCredentialsException e) {
        e.printStackTrace();
        return Result.fail(501, e.getMessage());
    }

    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public Result<Object> exceptionHandler2(MethodArgumentNotValidException e) {
        StringBuilder sb = new StringBuilder();
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        String message = allErrors.stream().map(s -> s.getDefaultMessage()).collect(Collectors.joining(";"));
        return Result.fail(5008, message);
    }

    @ExceptionHandler(MissingRequestValueException.class)
    public Result<Object> MissingRequestValueException(MissingRequestValueException e){
        e.printStackTrace();// 打印堆栈信息
        return Result.fail(5009, "请正确的传入参数");
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public Result<Object> ConstraintViolationException(ConstraintViolationException e){
        e.printStackTrace();// 打印堆栈信息
        String[] split = e.getMessage().split(",");
        String message = "";

        for (int i = 0; i < split.length; i++) {
            message += split[i].substring(split[i].indexOf(":")+1);
            if (split.length-1 != i){
                message += ",";
            }
        }
        return Result.fail(5016, message);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Result<Object> MethodArgumentNotValidException(MethodArgumentNotValidException e) {
        StringBuilder sb = new StringBuilder();
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        String message = allErrors.stream().map(s -> s.getDefaultMessage()).collect(Collectors.joining(";"));
        return Result.fail(502,message);
    }

}
